"""
Copyright (c) 2024 [Baijiankang/DUT]
简易火车订票系统

联系信息: [jiankang_bai@163.com]
项目地址: [https://gitee.com/BJKgitee/simple-train-ticket-booking-system]

保留所有权利。
"""

import json

def query_data_from_json(file_path, start_s, end_s):
    """
    查询JSON文件中某个数据的完整信息

    :param file_path: JSON文件的路径
    :param start_s: 起始站
    :param end_s: 终点站
    :return: 查询到的数据，如果没有找到则返回None
    """
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    for item in data:
        if item['start_s'] == start_s and item['end_s'] == end_s:
            return item

    return None

def add_station_to_json(file_path, start_s, end_s, insert_index, station_to_add, distance_to_add, time_to_add):
    """
    向JSON文件中添加特定部分的某个stations中的数据

    :param file_path: JSON文件的路径
    :param start_s: 起始站
    :param end_s: 终点站
    :param station_to_add: 要添加的车站
    :param distance_to_add: 要添加的距离
    :param time_to_add: 要添加的时间
    """
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    for item in data:
        if item['start_s'] == start_s and item['end_s'] == end_s:
            if station_to_add not in item['stations']:
                item['stations'].insert(insert_index, station_to_add)
                item['distance'].insert(insert_index, distance_to_add)
                item['distance'].insert(insert_index + 1, int(item['distance'].pop(insert_index+1)) - distance_to_add)
                item['time'].insert(insert_index, time_to_add)
                item['time'].insert(insert_index + 1, int(item['time'].pop(insert_index + 1)) - time_to_add)
                for route in item['forward']:
                    item['forward'][route].insert(insert_index, item['forward'][route][insert_index])
                for route in item['backward']:
                    item['backward'][route].insert(insert_index, item['backward'][route][insert_index - 1])
            break

    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

def modify_json_file(file_path, new_data, start_s, end_s):
    """
    修改JSON文件中的特定部分

    :param file_path: JSON文件的路径
    :param new_data: 新的数据，字典格式
    :param start_s: 起始站
    :param end_s: 终点站
    """

    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    for i, item in enumerate(data):
        if item['start_s'] == start_s and item['end_s'] == end_s:
            data[i] = new_data
            break

    with open(file_path, 'w',encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

def remove_station_from_json(file_path, start_s, end_s, station_to_remove):
    """
    从JSON文件中删除特定部分的某个stations中的数据

    :param file_path: JSON文件的路径
    :param start_s: 起始站
    :param end_s: 终点站
    :param station_to_remove: 要删除的车站
    """
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    for item in data:
        if item["start_s"] == start_s and item["end_s"] == end_s:
            if station_to_remove in item["stations"]:
                index_to_remove = item["stations"].index(station_to_remove)
                item["stations"].remove(station_to_remove)
                item["distance"].insert(index_to_remove, int(item["distance"].pop(index_to_remove))+int(item["distance"].pop(index_to_remove)))
                item["time"].insert(index_to_remove, int(item["time"].pop(index_to_remove))+int(item["time"].pop(index_to_remove)))
                for route in item['forward']:
                    item["forward"][route].insert(index_to_remove, min(int(item["forward"][route].pop(index_to_remove)), int(item["forward"][route].pop(index_to_remove))))
                for route in item['backward']:
                    item["backward"][route].insert(index_to_remove-1, min(int(item["backward"][route].pop(index_to_remove-1)), int(item["backward"][route].pop(index_to_remove-1))))
            break

    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)


def change_schedule(file_path, route, schedule):
    with open(file_path, 'r', encoding='utf-8') as file:
        alldata = json.load(file)

    for item in alldata:
        if route in item['forward']:
            item['forward'][route] = list(map(int, schedule))
            break
        if route in item['backward']:
            item['forward'][route] = list(map(int, schedule))
            break

    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(alldata, file, ensure_ascii=False, indent=4)